397. 整数替换 
为保证权益,题目请参考 397. 整数替换(From LeetCode).
解决方案1 
Python 
python
# 397. 整数替换
# https://leetcode-cn.com/problems/integer-replacement/
################################################################################
class Solution:
    def integerReplacement(self, n: int) -> int:
        count = 0
        while n != 1:
            if n == 3:
                count += 2
                break
            elif n % 2 == 0:
                n = n // 2
            else:
                if n & 2:
                    n = n + 1
                else:
                    n = n - 1
            count += 1
        return count
################################################################################
if __name__ == "__main__":
    solution = Solution()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28